import mainManage from "./mainManage";
import appConfig from "./appConfig";
import Tool from "./Tool";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class powerControlMediator extends cc.Component {

    @property({displayName : "体力出现的位置"})
    showPos : string = "";

    static powerPoint : number = 0;

    static getPowerTimes : number = 0;

    onLoad(){
        this.node.getChildByName("powerLab").getComponent(cc.Label).string = powerControlMediator.powerPoint + "";

        cc.game.on("setShowPower",()=>{
            this.node.getChildByName("powerLab").getComponent(cc.Label).string = powerControlMediator.powerPoint + "";
        });

        cc.game.on("setShowTimes",(min,second,close?)=>{
            if(close){
                this.setShowTime(min,second,close);
            }else{
                this.setShowTime(min,second);
            }
        })
    }

    protected onDestroy(): void {
        cc.game.off("setShowPower");
        cc.game.off("setShowTimes");
    }

    onClick(event,data){
        Tool.loadPrefab("framework/prefab/getPower",(res)=>{
            let view = cc.instantiate(res);
            view.parent = cc.find("Canvas");
        });
        console.log("展示获取体力界面");
    }


    setShowTime(min,second,close?){
        if(close){
            this.node.getChildByName("showTime").getComponent(cc.Label).string =  "";
            return;
        }
        if(second / 10 < 1){
            this.node.getChildByName("showTime").getComponent(cc.Label).string =  "0" + min + ":0" + second;
        }else{
            this.node.getChildByName("showTime").getComponent(cc.Label).string =  "0" + min + ":" + second;
        }
    }

    static addPower(powerValue){
        if(powerControlMediator.powerPoint >= appConfig.getPowerLimit()) return;
        if(powerControlMediator.powerPoint + powerValue > appConfig.getPowerLimit()){
            powerControlMediator.powerPoint = appConfig.getPowerLimit();
        }else{
            powerControlMediator.powerPoint += powerValue;
        }
        cc.game.emit("setShowPower");
        powerControlMediator.saveLocalPower();
    }

    static reducePower(success,fail?){
        if(powerControlMediator.powerPoint <= 0){
            mainManage.createToast("体力值不够 请获取体力值!");
            if(fail){
                fail();
            }
            Tool.loadPrefab("framework/prefab/getPower",(res)=>{
                let view = cc.instantiate(res);
                view.parent = cc.find("Canvas");
            });
            return;
        }
        powerControlMediator.powerPoint--;
        cc.game.emit("setShowPower");
        powerControlMediator.saveLocalPower();
        success();

    }

    static initPower(){
        let data = Tool.getData("newUser");
        if(data == null || data == undefined || data == ""){
            appConfig.setUserType(true);
            powerControlMediator.powerPoint = appConfig.getPowerLimit();
            Tool.saveData("newUser",appConfig.getUserType());
            Tool.saveData("userPower",powerControlMediator.powerPoint);
        }else{
            appConfig.setUserType(false);
            let power =  Tool.getData("userPower");
            if(power == null || typeof power == "undefined"){
                powerControlMediator.powerPoint = appConfig.getInitPower();
            }else{
                powerControlMediator.powerPoint = parseInt(power);
            }
        }
        setInterval(()=>{
            powerControlMediator.getPowerInterval();
        },1000);
    }

    static getPowerInterval(){
        new Promise(resolve => {
            let data = Tool.getData("saveTimes");
            let timeData = new Date();
            if(data == null && data == undefined || data == "" ){
                powerControlMediator.getPowerTimes = timeData.getTime();
                powerControlMediator.addPower(appConfig.getInitPower());
                Tool.saveData("saveTimes",powerControlMediator.getPowerTimes);
                powerControlMediator.saveLocalPower();
                resolve();
            }else{
                powerControlMediator.getPowerTimes = data;
                let dataInterval = timeData.getTime() - parseInt(data);
                //计算相差天数
                let day = Math.floor(dataInterval / (24 * 3600 * 1000));
                //计算相差小时
                let leave1 = dataInterval % (24 * 3600 * 1000);
                let hours = Math.floor(leave1/(3600*1000));
                //计算相差分钟
                let leave2 = leave1 % (3600 * 1000);
                let minutes = Math.floor(leave2 / (60 * 1000));
                if(hours > 0 || day > 0){
                    powerControlMediator.getLocalPower();
                    powerControlMediator.addPower(appConfig.getPowerLimit());
                    powerControlMediator.getPowerTimes = timeData.getTime();
                    Tool.saveData("saveTimes",powerControlMediator.getPowerTimes);
                    powerControlMediator.saveLocalPower();
                    console.log("增加" + appConfig.getPowerLimit() + "点体力值!",powerControlMediator.powerPoint);
                }else{
                    let addNumber = (minutes / appConfig.getPowerRecover()) | 0;
                    if(addNumber >= 1){
                        if(powerControlMediator.powerPoint + addNumber > appConfig.getPowerLimit()){
                            powerControlMediator.getLocalPower();
                            powerControlMediator.addPower(appConfig.getPowerLimit());
                            powerControlMediator.getPowerTimes = timeData.getTime();
                            Tool.saveData("saveTimes",powerControlMediator.getPowerTimes);
                            powerControlMediator.saveLocalPower();
                            console.log("增加" + appConfig.getPowerLimit() + "点体力值!",powerControlMediator.powerPoint);
                        }else {
                            powerControlMediator.getLocalPower();
                            powerControlMediator.addPower(addNumber);
                            powerControlMediator.getPowerTimes = timeData.getTime();
                            Tool.saveData("saveTimes",powerControlMediator.getPowerTimes);
                            powerControlMediator.saveLocalPower();
                            console.log("增加" + addNumber + "点体力值!",powerControlMediator.powerPoint);
                        }
                    }
                }
                resolve();
            }
        }).then(()=>{
            if(powerControlMediator.powerPoint >= appConfig.getPowerLimit()){
                cc.game.emit("setShowTimes",appConfig.getPowerRecover(),0,true);
                powerControlMediator.getPowerTimes = new Date().getTime();
                Tool.saveData("saveTimes",powerControlMediator.getPowerTimes);
                return;
            }
            let times = new Date().getTime() - powerControlMediator.getPowerTimes;
            let leave5 = times % (24 * 3600 * 1000);
            let leave6 = leave5 % (3600 * 1000);
            let leave7 = leave6 % (60*1000);
            let min = Math.floor(leave6 / (60 * 1000));
            let sec = Math.round(leave7/1000);
            if(sec == 0){
                cc.game.emit("setShowTimes",min,0);
            }else{
                cc.game.emit("setShowTimes",appConfig.getPowerRecover() - 1 - min,60 - sec);
            }
        })
    }

    static getLocalPower(){
        let power = Tool.getData("userPower");
        if(power == null || power == undefined || power == ""){
            powerControlMediator.powerPoint = 0;
        }else{
            powerControlMediator.powerPoint = parseInt(power);
        }
    }

    static saveLocalPower(){
        Tool.saveData("userPower",powerControlMediator.powerPoint);
    }
}
